<template>
	<el-dialog v-model="isVisible" :title="props.row?.edit ? '修改发货' : '订单发货'" width="600px" :before-close="handleClose">
		<el-form ref="formRef" class="good-service-form" :model="form" :rules="rules" label-width="80px" inline>
			<el-form-item label="发货商品">
				<el-radio-group v-model="form.constitute" class="w-full">
					<el-radio :value="1"> 全部商品 </el-radio>
					<el-radio :value="0"> 部分商品 </el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label-width="0" prop="product">
				<el-table
					ref="tableRef"
					v-if="form.constitute == 0"
					:data="goodsList"
					class="mb20"
					style="margin-left: 80px; width: 460px"
					@selection-change="onTableSelect"
				>
					<el-table-column type="selection" width="55" />
					<el-table-column label="商品ID" prop="id" min-width="100" align="center"> </el-table-column>
					<el-table-column label="商品名称" prop="id" min-width="200" align="center">
						<template #default="scope">
							<div class="flex-center text-left">
								<el-image :src="scope.row.skuImage || scope.row.productImage" style="width: 50px; height: 50px; flex-shrink: 0" />
								<div class="ml10">
									<p class="ellipsis2">{{ scope.row.productName }}</p>
									<p class="text-regular">物料编码：{{ scope.row.materialCode }}</p>
								</div>
							</div>
						</template>
					</el-table-column>
				</el-table>
			</el-form-item>

			<el-form-item label="快递名称" prop="name">
				<el-select v-model="form.name" placeholder="选择快递名称">
					<el-option v-for="item in constant.EXPRESSAG" :key="item.name" :label="item.name" :value="item.name" />
				</el-select>
			</el-form-item>
			<el-form-item label="快递单号" prop="no">
				<el-input v-model="form.no" placeholder="输入快递单号" @input="noInput" />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">取消</el-button>
				<el-button type="primary" v-debounce="submit">保存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { FormRules, ElMessage } from 'element-plus';
import { ref, reactive, watch, computed } from 'vue';
import { fetchOrderShipments } from '/@/api/order';
import constant from '/@/utils/constant';

const props = defineProps({
	// 列表内容
	visible: {
		type: Boolean,
		default: () => false,
	},
	row: {
		type: Object,
		default: () => null,
	},
});

const emit = defineEmits(['handleClose']);

const isVisible = ref(false);

const formRef = ref();
const form = ref<any>({
	constitute: 1,
});

const tableRef = ref();

//校验规则
const rules = reactive<FormRules<any>>({
	products: [{ required: true, trigger: 'blur', message: '发货商品不能为空' }],
	name: [{ required: true, trigger: 'blur', message: '快递名称不能为空' }],
	no: [{ required: true, trigger: 'blur', message: '快递单号不能为空' }],
});

watch(
	() => props.visible,
	(val) => {
		isVisible.value = val;
	},
	{
		immediate: true,
	}
);

const goodsList = computed(() => {
	const data = props.row?.product?.skus || props.row?.products || [];
	// const list = data.filter((item: any) => (props.row?.edit ? item.expressNo : !item.expressNo));
	return data;
});

const noInput = (val: any) => {
	form.value.no = val.replace(/[^a-zA-Z0-9]/g, '');
};

const onTableSelect = (value: any) => {
	form.value.products = value.map((v: any) => v.id);
};

const submit = async () => {
	formRef.value.validate(async (valid: boolean) => {
		if (valid) {
			if (form.value.constitute == 1) {
				form.value.products = goodsList.value.map((v: any) => v.id);
				if (!form.value.products.length) {
					ElMessage.warning('该订单不存在待发货商品');
					return;
				}
			}
			if (form.value.constitute == 0 && !form.value.products) {
				ElMessage.warning('请选择发货商品');
				return;
			}
			const res = await fetchOrderShipments({ ...form.value, id: props.row.orderId || props.row.id });
			if (res.code == 200) {
				ElMessage.success('订单发货完成');
				handleClose(true);
			} else {
				ElMessage.error(res.msg);
			}
		}
	});
};

const handleClose = (success?: boolean) => {
	form.value = {
		constitute: 1,
	};
	formRef.value.resetFields();
	emit('handleClose', success === true);
};
</script>

<style lang="scss">
.good-service-form {
	.el-form-item {
		width: 90%;
		.el-input-number {
			width: 100%;
		}
	}
}
</style>
